package com.yixun.qfbao.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author zilong.liu
 * @date 2021-1-19
 *
 * desc:
 *     
 */
public class UrlUtil {

	/**
	 * 把远程的URL连接转换为byte数组
	 * @param url
	 * @return
	 * @throws MalformedURLException
	 */
	public static byte[] urlTobyte(String url) throws MalformedURLException {  
	    URL ur = new URL(url);
	    BufferedInputStream in = null;
	    ByteArrayOutputStream out = null;
	    try {
	        in = new BufferedInputStream(ur.openStream());
	        out = new ByteArrayOutputStream(1024);
	        byte[] temp = new byte[1024];
	        int size = 0;
	        while ((size = in.read(temp)) != -1) {
	            out.write(temp, 0, size);
	        }
	    } catch (Exception e) {
	        e.printStackTrace();
	    } finally {
	        try {
	            in.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    }
	    byte[] content = out.toByteArray();
	    return content;
	}
}
